﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CITWebsite.Models
{
    public class UserProvider
    {
        private CITDBDataContext db = new CITDBDataContext();

        //insert
        public bool insert(USER user)
        {
            try
            {
                db.USERs.InsertOnSubmit(user);
                save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //delete
        public bool delete(string username)
        {
            try
            {
                USER user = db.USERs.SingleOrDefault(c => c.UserName == username);
                if (user != null)
                {
                    db.USERs.DeleteOnSubmit(user);
                    save();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        //update
        public bool update(int id, string username, string password, string fullname, string type, bool isactive)
        {
            try
            {
                USER user = db.USERs.SingleOrDefault(c => c.ID == id);
                user.UserName = username;
                user.Password = password;
                user.FullName = fullname;
                user.Type = type;
                user.IsActive = isactive;
                save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //save
        public void save()
        {
            if(db != null)
                db.SubmitChanges();
        }

        //cac truy van khac deu dung store
        //check login
        public bool checklogin(string username, string password)
        {
            USER[] lst = db.spUserCheckLogin(username, password).ToArray();
            if (lst.Length == 0)
                return false;
            return true;
        }

    }
}